//
//  FriendRequestListVC.m
//  TheProjectName
//
//  Created by 李劲成 on 16/5/23.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "FriendRequestListVC.h"
#import "FriendRequestCell.h"
@interface FriendRequestListVC ()<UITableViewDelegate, UITableViewDataSource>

// 展示申请列表
@property (nonatomic, retain) UITableView *m_tableView;
/** 本地获取的好友数据 */
@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation FriendRequestListVC



- (void)viewDidLoad {
    [super viewDidLoad];
    //根据当前用户名查询是否有好友申请
    self.dataSource = [NSMutableArray arrayWithArray:[[LocalCache sharedLocalCache]readArrayFromDiskWithKey:[EMClient sharedClient].currentUsername]];
    // 创建子视图
    [self createSubViews];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleRefresh:) name:@"receive" object:nil];
    // Do any additional setup after loading the view.
}

- (void)handleRefresh:(NSNotification *)nofi{
    //根据当前用户名查询是否有好友申请
    self.dataSource = [NSMutableArray arrayWithArray:[[LocalCache sharedLocalCache]readArrayFromDiskWithKey:[EMClient sharedClient].currentUsername]];
 
    [_m_tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self configNavi];
}
#pragma mark - 配置导航栏
- (void)configNavi {

    self.navigationItem.leftBarButtonItem = BARBUTTONIMAGE(@"aligh_left_27.291428571429px_1189116_easyicon.net", self, @selector(presentLeftMenuViewController:));
    self.navigationItem.title = @"申请列表";

}
#pragma mark - 创建子视图
- (void)createSubViews{
    self.m_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44) style:UITableViewStylePlain];
    _m_tableView.bounces = NO;
    _m_tableView.delegate = self;
    _m_tableView.dataSource = self;
    _m_tableView.rowHeight = 80;
    _m_tableView.separatorColor = [UIColor clearColor];
    _m_tableView.backgroundColor = [UIColor clearColor];
    [_m_tableView registerClass:[FriendRequestCell class] forCellReuseIdentifier:@"FriendRequest"];
    [self.view addSubview:_m_tableView];
}


#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FriendRequestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FriendRequest"];
    // 赋值
    [cell setName:_dataSource[indexPath.row] andMessage:nil];
    // 点击后回调
    cell.handleClick = ^(void){
        // 删除数据
        [_dataSource removeObjectAtIndex:indexPath.row];
        // 判断是否为空
        if (_dataSource.count == 0) {
            // 若为空 删除指定文件.
            [[LocalCache sharedLocalCache]removeFileWithKey:[EMClient sharedClient].currentUsername];
        }
        else{
        // 存入本地覆盖
        [[LocalCache sharedLocalCache] storeArray:_dataSource toDiskforKey:[EMClient sharedClient].currentUsername];
        }
        // 删除对应 CELL
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
    };
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
